<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="optionFormRules" :model="optionForm" label-position="left" label-width="140px">
      <el-form-item label="合约" prop="symbol">
        <el-input v-model="optionForm.symbol" />
      </el-form-item>
      <el-form-item label="类型" prop="style">
          <el-checkbox-group v-model="optionForm.style">
            <el-checkbox-button label="看涨" name="style"></el-checkbox-button>
            <el-checkbox-button label="看跌" name="style"></el-checkbox-button>
          </el-checkbox-group>
      </el-form-item>
      <el-form-item label="到期日" prop="expire_date">
        <el-input v-model="optionForm.expire_date" />
      </el-form-item>
      <el-form-item label="行权价" prop="exercise_price">
        <el-input v-model="optionForm.exercise_price" />
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="optionForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="optionForm.status.value === 5" type="danger" @click="handleDelete(optionForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveOption, removeOptionById, getOption } from '@/api/knowledge/option'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'

export default {
  name: 'ComplexTable',
  components: { },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      optionForm: {
        code: '',
        title: '',
        remark: '',
        status: {
          label: '',
          value: 1
        }
      },
      optionFormRules: []
    }
  },
  created() {
    let optionId = this.$route.query && this.$route.query.optionId
    if (optionId !== undefined) {
      optionId = parseInt(optionId)
      if (!isNaN(optionId)) {
        this.listLoading = true
        const formData = {
          'optionId': optionId
        }
        getOption(formData).then(response => {
          if (response.status === 200) {
            this.optionForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(optionId)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'option' })
    },
    createData() {
      this.listLoading = true
      const optionForm = this.optionForm
      optionForm.status = optionForm.status.value
      console.log(optionForm)
      if (optionForm.identity > 0) {
        optionForm.optionId = optionForm.identity
      }
      saveOption(optionForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'option' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const fromdata = {
        'optionId': row.identity
      }
      removeOptionById(fromdata).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'option' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
